package org.niu.tree;

import java.util.*;

public class Print {
    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        Queue<TreeNode> q = new LinkedList<>();
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        q.offer(pRoot);
        int cnt = 1;
        while(!q.isEmpty()){
            int size = q.size();
            ArrayList<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < size;i++){
                TreeNode head = q.poll();
                tmp.add(head.val);
                if(head.left != null) q.offer(head.left);
                if(head.right != null) q.offer(head.right);
            }
            if((cnt&1) == 0) Collections.reverse(tmp);
            ans.add(tmp);
            cnt++;
        }
        return ans;
    }
}
